#include "MySqlUpdateStatement.h"

#include "../../../../ast/SQLLimit.h"
#include "../../../../ast/SQLName.h"
#include "../../../../ast/SQLCommentHint.h"
#include "../../../../ast/SQLExpr.h"
#include "../../../../ast/statement/SQLUpdateSetItem.h"
#include "../../../../dialect/mysql/visitor/MySqlASTVisitor.h"
#include "../../../../visitor/SQLASTVisitor.h"
#include "../../../../../utils/log.h"
#include "../../../../../utils/listAddAll.h"
#include "../../../../../utils/instanceof.h"
#include "../../../../../utils/ifeq.h"
#include "../../../../ast/statement/SQLTableSource.h"

MySqlUpdateStatement::MySqlUpdateStatement()
    : SQLUpdateStatement(DbType_ptr(new DbType(&DbType::mysql)))
{
  hints = std::make_shared<std::list<SQLCommentHint_ptr>>();
}

SQLLimit_ptr MySqlUpdateStatement::getLimit()
{
  return limit;
}

void MySqlUpdateStatement::setLimit(SQLLimit_ptr limit)
{
  if (limit != NULL)
  {
    limit->setParent(SharedObject(MySqlUpdateStatement));
  }
  this->limit = limit;
}

void MySqlUpdateStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, MySqlASTVisitor>(visitor.get()))
  {
    MySqlASTVisitor_ptr tmp = std::dynamic_pointer_cast<MySqlASTVisitor>(visitor);
    accept0(tmp);
  }
  else
  {
    SQLUpdateStatement::accept0(visitor);
  }
}

void MySqlUpdateStatement::accept0(MySqlASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(MySqlUpdateStatement)))
  {
    if (tableSource != NULL)
    {
      tableSource->accept(visitor);
    }

    if (from != NULL)
    {
      from->accept(visitor);
    }

    if (!items->empty())
    {
      auto it_items = items->begin();
      for (int i = 0; i < items->size(); i++)
      {
        std::advance(it_items, i);
        SQLUpdateSetItem_ptr item = *it_items;
        if (item != NULL)
        {
          item->accept(visitor);
        }
      }
    }

    if (where != NULL)
    {
      where->accept(visitor);
    }

    if (orderBy != NULL)
    {
      orderBy->accept(visitor);
    }

    if (limit != NULL)
    {
      limit->accept(visitor);
    }

    if (!hints->empty())
    {
      auto it_hints = hints->begin();
      for (int i = 0; i < hints->size(); i++)
      {
        std::advance(it_hints, i);
        SQLCommentHint_ptr hint = *it_hints;
        if (hint != NULL)
        {
          hint->accept(visitor);
        }
      }
    }
  }
  visitor->endVisit(SharedObject(MySqlUpdateStatement));
}

BOOL_ptr MySqlUpdateStatement::isLowPriority()
{
  return lowPriority;
}

void MySqlUpdateStatement::setLowPriority(BOOL_ptr lowPriority)
{
  this->lowPriority = lowPriority;
}

BOOL_ptr MySqlUpdateStatement::isIgnore()
{
  return ignore;
}

void MySqlUpdateStatement::setIgnore(BOOL_ptr ignore)
{
  this->ignore = ignore;
}

BOOL_ptr MySqlUpdateStatement::isCommitOnSuccess()
{
  return commitOnSuccess;
}

void MySqlUpdateStatement::setCommitOnSuccess(BOOL_ptr commitOnSuccess)
{
  this->commitOnSuccess = commitOnSuccess;
}

BOOL_ptr MySqlUpdateStatement::isRollBackOnFail()
{
  return rollBackOnFail;
}

void MySqlUpdateStatement::setRollBackOnFail(BOOL_ptr rollBackOnFail)
{
  this->rollBackOnFail = rollBackOnFail;
}

BOOL_ptr MySqlUpdateStatement::isQueryOnPk()
{
  return queryOnPk;
}

void MySqlUpdateStatement::setQueryOnPk(BOOL_ptr queryOnPk)
{
  this->queryOnPk = queryOnPk;
}

SQLExpr_ptr MySqlUpdateStatement::getTargetAffectRow()
{
  return targetAffectRow;
}

void MySqlUpdateStatement::setTargetAffectRow(SQLExpr_ptr targetAffectRow)
{
  if (targetAffectRow != NULL)
  {
    targetAffectRow->setParent(SharedObject(MySqlUpdateStatement));
  }
  this->targetAffectRow = targetAffectRow;
}

bool MySqlUpdateStatement::equals(Object_ptr o)
{
  if (this == o.get())
  {
    return true;
  }
  if (o == NULL || getClass() != o->getClass())
  {
    return false;
  }
  if (!SQLUpdateStatement::equals(o))
  {
    return false;
  }

  std::shared_ptr<MySqlUpdateStatement> that = std::dynamic_pointer_cast<MySqlUpdateStatement>(o);

  if (lowPriority != that->lowPriority)
  {
    return false;
  }
  if (ignore != that->ignore)
  {
    return false;
  }
  if (commitOnSuccess != that->commitOnSuccess)
  {
    return false;
  }
  if (rollBackOnFail != that->rollBackOnFail)
  {
    return false;
  }
  if (queryOnPk != that->queryOnPk)
  {
    return false;
  }
  if (!this->hints->empty() ? !(hints == that->hints) : !that->hints->empty())
  {
    return false;
  }
  if (limit != NULL ? !limit->equals(that->limit) : that->limit != NULL)
  {
    return false;
  }
  return targetAffectRow != NULL ? targetAffectRow->equals(that->targetAffectRow) : that->targetAffectRow == NULL;
}

int MySqlUpdateStatement::hashCode()
{
  int result = SQLUpdateStatement::hashCode();
  result = 31 * result + (limit != NULL ? limit->hashCode() : 0);
  result = 31 * result + (lowPriority ? 1 : 0);
  result = 31 * result + (ignore ? 1 : 0);
  result = 31 * result + (commitOnSuccess ? 1 : 0);
  result = 31 * result + (rollBackOnFail ? 1 : 0);
  result = 31 * result + (queryOnPk ? 1 : 0);
  result = 31 * result + (targetAffectRow != NULL ? targetAffectRow->hashCode() : 0);
  // result = 31 * result + (!hints.empty() ? hints.hashCode() : 0);
  return result;
}

SQLName_ptr MySqlUpdateStatement::getForcePartition()
{
  return forcePartition;
}

void MySqlUpdateStatement::setForcePartition(SQLName_ptr x)
{
  if (x != NULL)
  {
    x->setParent(SharedObject(MySqlUpdateStatement));
  }
  this->forcePartition = x;
}

BOOL_ptr MySqlUpdateStatement::isForceAllPartitions()
{
  return forceAllPartitions;
}

void MySqlUpdateStatement::setForceAllPartitions(BOOL_ptr forceAllPartitions)
{
  this->forceAllPartitions = forceAllPartitions;
}

int MySqlUpdateStatement::getHintsSize()
{
  if (hints->empty())
  {
    return 0;
  }

  return hints->size();
}

SQLCommentHint_list_ptr MySqlUpdateStatement::getHints()
{
  // if (hints.empty()) {
  //     hints = new ArrayList<SQLCommentHint*>(2);
  // }
  return hints;
}

void MySqlUpdateStatement::setHints(SQLCommentHint_list_ptr hints)
{
  this->hints->clear(); // = hints;
  Utils::ListAddAll<SQLCommentHint, SQLCommentHint>(hints, this->hints);
}
